/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mrs.alliance;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import mrs.alliance.impl.Message;
import mrs.alliance.impl.MessageService;

/**
 *
 * @author Vitaljok
 */
public class MessageServicePanel extends javax.swing.JPanel {

    MessageService service;
    
    /**
     * Creates new form MessageServicePanel
     */
    public MessageServicePanel() {
        initComponents();
        
        TableModel model = new AbstractTableModel() {

            @Override
            public int getRowCount() {                
                return service.getMessages().size();
            }

            @Override
            public int getColumnCount() {
                return 3;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                Message msg = service.getMessages().get(rowIndex);
                switch (columnIndex) {
                    case 0:                        
                        return msg.getTo();
                    case 1: 
                        return msg.getMessage();
                    case 2:
                        return msg.getTimeToLive();
                }                
                return null;
            }
        };
        
        this.msgTable.setModel(model);
    }  

    public MessageService getService() {
        return service;
    }

    public void setService(MessageService service) {
        this.service = service;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        msgTable = new javax.swing.JTable();

        msgTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "To", "Message", "TTL"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(msgTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable msgTable;
    // End of variables declaration//GEN-END:variables
}
